<?php
namespace app\common\controller;

use app\common\extra\Status;
use app\common\model\FileModel;
use support\Request;

class Upload
{

    public function upload(Request $request){
        /**
         * @var \Webman\Http\UploadFile
         */
        $fileData = $request->file('file');

        if(!empty($fileData->getUploadErrorCode())){
            return jsonError(Status::CODE_INTERNAL_SERVER_ERROR , [] ,'no file upload' );
        }

        $fileBaseInfo = [
            'file_type' => $fileData->getUploadMineType(),
            'file_size' => $fileData->getSize(),
            'filename' => $fileData->getUploadName(),
            'md5_file' => $fileData->getMD5File(),
            'suffix' => $fileData->getSuffix(),//文件后缀
        ];

        //判断目录是否存在
        $dirPath = '.' . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "upload" . DIRECTORY_SEPARATOR . date("Ymd");
        $suffix = $fileBaseInfo['suffix'];//扩展名
        $filePath = $dirPath . DIRECTORY_SEPARATOR . $fileData->getSaveName();

        //判断文件是否已经存在了
        $fileModel = new FileModel();
        $isExist = $fileModel->isExistFile($fileBaseInfo['md5_file']);
        $imagewidth = $imageheight = 0;
        if(!$isExist){
            //创建目录
            if(!is_dir($dirPath)){
                if(!mkdir($dirPath , 0700 , true)){
                    return jsonError(Status::CODE_INTERNAL_SERVER_ERROR);
                }
            }
            if(!$fileData->move($filePath)){
                return jsonError(Status::CODE_INTERNAL_SERVER_ERROR , [] , "save file fail");
            }
            //如果是图片则获取其宽度和高度
            if(in_array(strtolower($suffix), ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) ){
                $imgInfo = getimagesize($filePath);
                $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
                $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
            }
            $fileBaseInfo['url'] = $filePath;

        }else{
            $fileBaseInfo['url'] = $isExist->url;
            $imagewidth = $isExist->imagewidth;
            $imageheight = $isExist->imageheight;
        }

        $saveData = [
            'admin_id' => 0,
            'url' => str_replace('.'.DIRECTORY_SEPARATOR , DIRECTORY_SEPARATOR , $fileBaseInfo['url']),
            'imagewidth' => $imagewidth,
            'imageheight' => $imageheight,
            'file_type' => $imageheight,
            'mimetype' => $fileBaseInfo['file_type'],
            'file_size' => $fileBaseInfo['file_size'],
            'filename' => $fileBaseInfo['filename'],
            'md5_file' => $fileBaseInfo['md5_file'],
        ];

        try{
            $result = $fileModel->save($saveData);
            if($result === false){
                throw new \Exception("上传失败");
            }
        }catch(\Exception $exception){
            $errMsg = $exception->getMessage();
            return jsonError(Status::CODE_BAD_GATEWAY , ['name' => 1] , $errMsg );
        }

        return jsonSuccess(Status::CODE_OK , [
            'url' => $saveData['url'],
            'file_type' => $saveData['file_type'],
            'file_size' => $saveData['file_size'],
        ] , "success");

    }

}